using System;
using System.Globalization;
using System.Reflection;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;

namespace Microsoft.WPF.Samples
{
    /// <summary>
    /// A converter to load a default background brush for a card based on the 
    /// gender.
    /// </summary>
    [ValueConversion(typeof(String), typeof(DrawingBrush))]
    public class GenderToDrawingBrush 
        : IValueConverter
    {
        #region Convert()
        /// <summary>
        /// Converts the male value into a DrawingBrush from the 
        /// ResourceDictionary.
        /// </summary>
        /// <param name="value">The value produced by the binding source.
        /// </param>
        /// <param name="targetType">The type of the binding target property.
        /// </param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>
        /// A converted value. If the method returns null, the valid null value 
        /// is used.
        /// </returns>
        public object Convert(
            object value,
            Type targetType,
            object parameter,
            CultureInfo culture)
        {
            var assemblyName = Assembly.GetExecutingAssembly();

            var packUri =
                string.Format(
                    "/{0};component/{1}",
                    assemblyName.FullName,
                    "ResourceDictionary.xaml");

            var uri = new Uri(packUri, UriKind.Relative);

            var resObj = Application.LoadComponent(uri);

            if (resObj == null)
            {
                return new SolidColorBrush(
                    Color.FromRgb(
                        255,
                        255,
                        255));
            }

            var dict = resObj as ResourceDictionary;

            if (dict == null)
            {
                return new SolidColorBrush(
                    Color.FromRgb(
                        255,
                        255,
                        255));
            }

            switch (value.ToString())
            {
                case "male":
                    return dict["DrawingBrushMale"];

                case "female":
                    return dict["DrawingBrushFemale"];

                default:
                    return null;
            }
        } 
        #endregion

        #region ConvertBack()
        /// <summary>
        /// Converts a value.
        /// </summary>
        /// <param name="value">The value that is produced by the binding 
        /// target.</param>
        /// <param name="targetType">The type to convert to.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>
        /// A converted value. 
        /// If the method returns null, the valid null value is used.
        /// </returns>
        public object ConvertBack(
            object value,
            Type targetType,
            object parameter,
            CultureInfo culture)
        {
            return null;
        } 
        #endregion
    }
}